package tech.lq0.dreamaticdelight.item.food.chiram;

import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import tech.lq0.dreamaticdelight.init.DMDItems;
import tech.lq0.dreamaticvoyage.tools.Livers;
import tech.lq0.dreamaticvoyage.tools.TooltipTool;
import vectorwing.farmersdelight.common.item.ConsumableItem;

import java.util.List;

public class ChiramPenumbra extends ConsumableItem {

    private static final FoodProperties FOOD = (new FoodProperties.Builder()).nutrition(8).saturationMod(0.6F)
            .effect(() -> new MobEffectInstance(MobEffects.CONFUSION, 3600, 0), 1.0F)
            .effect(() -> new MobEffectInstance(MobEffects.DARKNESS, 2400, 0), 1.0F)
            .effect(() -> new MobEffectInstance(MobEffects.HUNGER, 2400, 3), 1.0F)
            .effect(() -> new MobEffectInstance(MobEffects.POISON, 2400, 3), 1.0F)
            .effect(() -> new MobEffectInstance(MobEffects.WITHER, 2400, 3), 1.0F)
            .alwaysEat()
            .build();

    public ChiramPenumbra() {
        super(new Properties().food(FOOD).stacksTo(1).craftRemainder(Items.GLASS_BOTTLE), true);
    }

    @Override
    public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag isAdvanced) {
        tooltip.add(Component.translatable("des.dreamaticdelight.chiram_penumbra").withStyle(ChatFormatting.GRAY).withStyle(ChatFormatting.ITALIC));
        super.appendHoverText(stack, level, tooltip, isAdvanced);
        TooltipTool.addLiverInfo(tooltip, Livers.CHIRAM);
    }

    @Override
    public ItemStack finishUsingItem(ItemStack pStack, Level pLevel, LivingEntity pLivingEntity) {
        if (!pLevel.isClientSide) {
            pLivingEntity.hurt(pLevel.damageSources().magic(), 20.0F);
        }

        return super.finishUsingItem(pStack, pLevel, pLivingEntity);
    }

    @Override
    public void inventoryTick(ItemStack pStack, Level pLevel, Entity pEntity, int pSlotId, boolean pIsSelected) {
        if (!pLevel.isClientSide && pEntity instanceof Player player) {
            if (pLevel.random.nextDouble() < 0.0005D) {
                player.getInventory().setItem(pSlotId, new ItemStack(DMDItems.CHIRAM_UMBRA.get(), 1));
            }
        }

        super.inventoryTick(pStack, pLevel, pEntity, pSlotId, pIsSelected);
    }
}
